package com.example.resourceSharingPlatform.dao;

import com.example.resourceSharingPlatform.entity.Item;
import com.example.resourceSharingPlatform.mappers.ItemRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据id查询item信息
     * parameter: int id
     * */
    public Item queryItemById(int id){
        String sql = "SELECT * FROM item WHERE id=?";
        Item item = jdbcTemplate.queryForObject(sql, new ItemRowMapper(), id);
        return item;
    }

    /**
     *  根据owner_id查询items信息
     * */
    public List<Item> queryItemsByOwnerId(String id){
        String sql = "SELECT * FROM item WHERE owner_id=?";
        List<Item> itemList = jdbcTemplate.query(sql, new ItemRowMapper(), id);
        return itemList;
    }

    /**
     * 根据name查询item信息
     * parameter: String name
     * */
    public List<Item> queryItemByName(String name){
        String sql = "SELECT * FROM item WHERE name=? ";
        List<Item> itemList = jdbcTemplate.query(sql, new ItemRowMapper(), name);
        return itemList;
    }

    /**
     * 根据type查询items信息
     * parameter: String type
     * */
    public List<Item> queryItemByType(String type){
        String sql = "SELECT * FROM item WHERE type=? ";
        List<Item> itemList = jdbcTemplate.query(sql, new ItemRowMapper(), type);
        return itemList;
    }

    /**
     * 按照租借状态查询item信息
     * @param borrow_state
     * 可租借 allow_borrow
     * 已租借 borrowed
     * */
    public List<Item> queryItemByBorrowState(String borrow_state, String type){
        String sql = "SELECT * FROM item WHERE borrow_state = ? AND type=?";
        List<Item> itemList = jdbcTemplate.query(sql, new ItemRowMapper(), borrow_state, type);
        return itemList;
    }

    /**
     * 查询所有item信息
     * parameter: int id
     * */
    public List<Item> queryAll(){
        String sql = "SELECT * FROM item";
        List<Item> itemList = jdbcTemplate.query(sql, new ItemRowMapper());
        return itemList;
    }

    /**
     * 插入一条item信息
     * parameter: Item item
     * */
    public Item insertItem(Item item){
        String sql = "INSERT INTO item VALUES(?,?,?,?,?,?,?,?,?,?)";
        updateByItem(sql, item);
        String sql2 = "SELECT * FROM item WHERE id=(SELECT MAX(id) FROM item)";
        return jdbcTemplate.queryForObject(sql2,new ItemRowMapper());
    }

    /**
     * 更新一条item信息
     * parameter: Item item
     * */
    public boolean updateItem(Item item){
        String sql = "UPDATE item " +
                "SET id=?, name=?, type=?, owner_id=?, borrow_state=?, " +
                "check_state=?, description=?, picture=?, price=?, valuation=?" +
                "WHERE id=" + item.getId();
        int row = updateByItem(sql, item);
        return row != 0;
    }

    /**
     * 根据id删除一条item信息
     * parameter: Item item
     * */
    public boolean deleteItemById(int id){
        String sql = "DELETE FROM item WHERE id=?";
        int row = jdbcTemplate.update(sql,id);
        return row!= 0;
    }

    private int updateByItem(String sql, Item item){
        return jdbcTemplate.update(sql, item.getId(), item.getName(), item.getType(),
                item.getOwner_id(), item.getBorrow_state(), item.getCheck_state(),
                item.getDescription(), item.getPicture(), item.getPrice(), item.getValuation());
    }
}
